package com.apes.fn.scm.crossCompany.transfer.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.crossCompany.transfer.model
 * @ClassName: CrossCompanyTransferItemNew
 * @Author: linwz
 * @CreateTime: 2021-04-07 16:33
 * @Description: 跨公司调拨明细
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = CrossCompanyTransferItemNew.CONST_TABLE_NAME)
public class CrossCompanyTransferItemNew extends BaseModel {
    public static final String CONST_TABLE_NAME = "cross_company_tran_item_new";
    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "跨公司调拨单明细")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "transfer_id", referencedColumnName = "id")
    @Comment("调拨单号")
    private CrossCompanyTransferNew crossCompanyTransfer;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;

    @Column(name = "product_sum_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("可调拨数量")
    private double productSumQty = 0;

    @Column(name = "product_sum", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("调拨数量")
    private double productSum = 0;

    @Column(name = "ship_sum", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("发货数量")
    private double ShipNum = 0;

    @Column(name = "arriva_sum", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("到货数量")
    private double arrivalSum = 0;

    @Column(name = "finish_sum", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("终止数量")
    private double finishSum = 0;

    @Column(name = "finish_reason")
    @Comment("终止原因")
    private String finishReason;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "origin_item_id")
    @Comment("原始明细编码")
    private String originItemId;

    @Column(name = "supplier_id", length = 4000)
    @Comment("供应商")
    private String supplierId;

    @Transient
    private double factor;

    public double getFactor() {
        if (uom != null) return 1/uom.getFactor();
        return 0;
    }

    @Transient
    private double withDrawDiveryQty = 0;

    public double getWithDrawDiveryQty() {
        return this.withDrawDiveryQty;
    }
}
